package com.zjy.org.service.impl;

import com.zjy.basic.service.BaseService;
import com.zjy.basic.service.impl.BaseServiceImpl;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.Md5Utils;
import com.zjy.basic.util.PageList;
import com.zjy.basic.util.StrUtils;
import com.zjy.org.domain.Employee;
import com.zjy.org.mapper.EmployeeMapper;
import com.zjy.org.query.EmployeeQuery;
import com.zjy.org.service.EmployeeService;
import com.zjy.user.domain.Logininfo;
import com.zjy.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);//0是管理员
        logininfo.setDisable(true);

        //将logininfo对象添加到数据库，并生成自增长id
        logininfoMapper.add(logininfo);//获取到自增ID
        //将logininfo_id设置到对象中
        employee.setLogininfo_id(logininfo.getId());

        //添加数据
        super.add(employee);

        //为当前员工添加角色信息
        if (employee.getRole_id() != null) {
            employeeMapper.addRole(employee);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        /*
        外键：关联字段 + 外键约束
        如果只有关联字段，没有约束可以随便删
        但是如果有关联字段，也有外键约束 - 先删除关联信息，再删除当前表的数据
        开发中：先添加关联字段，不添加外键约束。开发完成之后将约束条件加进去。-方便开发时测试
        */
        //删除角色信息
        employeeMapper.delEmployeeRoleByEmployeeId(id);

        //查询对象
        Employee employee = employeeMapper.findById(id);
        //删除logininfo信息
        logininfoMapper.del(employee.getLogininfo_id());
        //删除信息
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        //前端传过来的对象是没有登陆信息id
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //获取原来的对象
        Employee oldEmployee = employeeMapper.findById(employee.getId());//loginfo_id
        //修改信息
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());

        //更新数据
        super.update(employee);

        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.findById(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee, oldLogininfo);
        //设置会以前的id - 进行修改
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //修改当前员工的角色
        if (employee.getRole_id()!=null){
            //删除角色信息
            employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
            //再添加新的角色信息
            employeeMapper.addRole(employee);
        }
    }

    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.loadByShopId(shopId);
    }
}
